package com.sqltoy.helloworld.service;

import java.time.LocalDateTime;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.sagacity.sqltoy.config.model.Translate;
import org.sagacity.sqltoy.dao.LightDao;
import org.sagacity.sqltoy.model.EntityQuery;
import org.sagacity.sqltoy.model.MapKit;
import org.sagacity.sqltoy.model.QueryExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.alibaba.fastjson2.JSON;
import com.sqltoy.helloworld.dto.OrderInfoVO;
import com.sqltoy.helloworld.entity.OrderInfo;

@SuppressWarnings("unchecked")
@SpringBootTest
public class TranslateTest {

	@Autowired
	LightDao lightDao;

	/**
	 * 代码中直接传sql模式
	 */
	@Test
	public void testTranslate() {
		String sql = """
				select t.*,
				       -- 额外扩展两个查询属性，用于映射到VO的名称属性上
				       t.order_type orderTypeName,
				       t.organ_id organName
				from SQLTOY_ORDER_INFO t
				where 1=1
				#[and t.status in (:statusAry)]
				#[and t.create_time>=:beginTime]
				#[and t.create_time<=:endTime]
				""";

		List<OrderInfoVO> result = lightDao.findByQuery(new QueryExecutor(sql)
				.translates(new Translate("dictKeyNameCache").setCacheType("ORDER_TYPE").setColumn("orderTypeName"),
						new Translate("organIdNameCache").setColumn("organName"))
				.values(MapKit.keys("statusAry", "beginTime", "endTime").values(new Integer[] { 1 },
						LocalDateTime.parse("2024-10-17T00:00:01"), null))
				.resultType(OrderInfoVO.class)).getRows();
		System.err.println(JSON.toJSONString(result));
	}

	/**
	 * sqltoy提供了单表查询不写完整sql的模式,但where条件需自己写，可以写表字段也可以写成POJO属性名称
	 */
	@Test
	public void testTranslateByEntity() {
		List<OrderInfoVO> result = lightDao.findEntity(OrderInfo.class,
				// select * 场景无需写.select()这里仅仅示范一下
				// EntityQuery.create().unselect(排除的字段)
				EntityQuery.create().select()
						// 沿用sqltoy动态条件的规则
						.where("#[and status in (:statusAry)]#[and createTime>=:beginTime]#[and createTime<=:endTime]")
						.translates(
								new Translate("dictKeyNameCache").setCacheType("ORDER_TYPE").setColumn("orderTypeName")
										.setKeyColumn("orderType"),
								new Translate("organIdNameCache").setColumn("organName").setKeyColumn("organId"))
						.values(MapKit.keys("statusAry", "beginTime", "endTime").values(new Integer[] { 1 },
								LocalDateTime.parse("2024-10-17T00:00:01"), null)),
				OrderInfoVO.class);
		System.err.println(JSON.toJSONString(result));
	}

	/**
	 * 在OrderInfoVO属性:organName、orderTypeName上加注解模式实现缓存翻译 如:organName
	 * 上加@Translate(cacheName = "organIdNameCache", keyField = "organId")
	 */
	@Test
	public void testTranslateByEntityAnnotationTrans() {
		List<OrderInfoVO> result = lightDao.findEntity(OrderInfo.class,
				// select * 场景无需写.select()这里仅仅示范一下
				// EntityQuery.create().unselect(排除的字段)
				EntityQuery.create().select()
						// 沿用sqltoy动态条件的规则
						.where("#[and status in (:statusAry)]#[and createTime>=:beginTime]#[and createTime<=:endTime]")
						.values(MapKit.keys("statusAry", "beginTime", "endTime").values(new Integer[] { 1 },
								LocalDateTime.parse("2024-10-17T00:00:01"), null)),
				OrderInfoVO.class);
		System.err.println(JSON.toJSONString(result));
	}

	/**
	 * sql放在xml中的模式
	 *
	 */
	// sql参见com/sqltoy/helloworld/sqltoy/sqltoy-helloworld.sql.xml 文件,sqlId要全局唯一
	@Test
	public void testTranslateWithSqlId() {
		List<OrderInfoVO> result = lightDao.find("helloworld_search_orderInfo",
				MapKit.keys("statusAry", "beginTime", "endTime").values(new Integer[] { 1 },
						LocalDateTime.parse("2024-10-17T00:00:01"), null),
				OrderInfoVO.class);
		System.err.println(JSON.toJSONString(result));
	}

	// lambda模式，请参见第三方插件:https://gitee.com/momoljw/sqltoy-lambda 或
	// https://gitee.com/gzghde/sqltoy-plus
}
